BlankClassSoDocsWillBeGenerated

version(D_Ddoc)
class BlankClassSoDocsWillBeGenerated

Examples

This example is for the CHECK constraint. The table in SQL can be created by $(D $(D sql CREATE TABLE Orders ( Id INTEGER NOT NULL PRIMARY KEY, OrderPrice INTEGER CHECK(OrderPrice > 0), Customer TEXT ); ))

1 import db_constraints;
2 
3 class Order
4 {
5     private int _Id;
6     // marking Id with not null and primary key
7     @NotNull @PrimaryKeyColumn
8     @property int Id()
9     {
10         return _Id;
11     }
12     @property void Id(int value)
13     {
14         setter(_Id, value);
15     }
16 
17     private int _OrderPrice;
18     // marking OrderPrice with a check constraint
19     // that makes sure OrderPrice is greater than 0
20     @CheckConstraint!(a => a > 0)
21     @property int OrderPrice()
22     {
23         return _OrderPrice;
24     }
25     @property void OrderPrice(int value)
26     {
27         setter(_OrderPrice, value);
28     }
29 
30     private string _Customer;
31     @property string Customer()
32     {
33         return _Customer;
34     }
35     @property void Customer(string value)
36     {
37         setter(_Customer, value);
38     }
39 
40     this(int Id_, int OrderPrice_, string Customer_)
41     {
42         this._Id = Id_;
43         this._OrderPrice = OrderPrice_;
44         this._Customer = Customer_;
45         initializeKeyedItem();
46     }
47 
48     mixin KeyedItem!();
49 }
50 
51 import std.exception : assertNotThrown, assertThrown;
52 
53 // throws because -10 is less than 0 and the check constraint does
54 // not allow that.
55 assertThrown!CheckConstraintException(new Order(1, -10, "Johnson"));
56 
57 // we can create a new order that will not error
58 auto i = new Order(1, 10, "Johnson");
59 // if we change the order price to another value still greater than
60 // 0 we should not get an error
61 assertNotThrown!CheckConstraintException(i.OrderPrice = 9);
62 assert(i.OrderPrice == 9);
63 
64 // but we will get an error if we try to change it to
65 // something below 0 again
66 assertThrown!CheckConstraintException(i.OrderPrice = -1);

Meta